﻿using MediumApi.Result;
using MediumApi.Tools;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MediumApi
{
    public class BaseController : ControllerBase
    {
        private string _userID;
        private int _roleID;
        /// <summary>
        /// token UserID
        /// </summary>
        public string UserID
        {
            get
            {
                if (string.IsNullOrEmpty(_userID))
                    GetToken();
                return _userID;
            }
        }
        public int RoleID
        {
            get
            {
                if (_roleID == 0)
                    GetToken();
                return _roleID;
            }
        }
        /// <summary>
        /// 获取Token信息
        /// </summary>
        /// <returns></returns>
        private void GetToken()
        {
            this._userID = string.Empty;
            var authHeader = HttpContext.Request.Headers["Token"];
            var userinfo = TokenHelpers.DecodeToObject(authHeader);
            if (userinfo == null) 
            {
                this._userID = "008b4c240b064fbe92db1ff2e58dd99c";
                return;
            } 
            this._userID = userinfo.UserId;
            this._roleID = userinfo.RoleId;
        }
        /// <summary>
        /// 返回成功
        /// </summary>
        /// <returns></returns>
        protected OperateResult Success()
        {
            OperateResult res = new OperateResult
            {
                Success = true,
                Msg = "请求成功！",
                ErrorCode = 200
            };

            return res;
        }
        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <returns></returns>
        protected OperateResult<T> Success<T>(T data)
        {
            OperateResult<T> res = new OperateResult<T>
            {
                Success = true,
                Msg = "操作成功",
                Data = data,
                ErrorCode = 200
            };

            return res;
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        protected OperateResult<T> Success<T>(T data, string msg)
        {
            OperateResult<T> res = new OperateResult<T>
            {
                Success = true,
                Msg = msg,
                Data = data,
                ErrorCode = 200
            };

            return res;
        }

        /// <summary>
        /// 返回错误
        /// </summary>
        /// <returns></returns>
        protected OperateResult Error()
        {
            OperateResult res = new OperateResult
            {
                Success = false,
                Msg = "请求失败！",
                ErrorCode = 500
            };

            return res;
        }

        /// <summary>
        /// 返回错误
        /// </summary>
        /// <param name="msg">错误提示</param>
        /// <returns></returns>
        protected OperateResult Error(string msg)
        {
            OperateResult res = new OperateResult
            {
                Success = false,
                Msg = msg,
                ErrorCode = 500
            };
            return res;
        }


        /// <summary>
        /// 返回错误
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        protected OperateResult<T> Error<T>(T data, string msg)
        {
            OperateResult<T> res = new OperateResult<T>
            {
                Msg = msg,
                Data = data,
                Success = false,
                ErrorCode = 500
            };

            return res;
        }
        /// <summary>
        /// 分页实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class PagedResult<T>
        {
            /// <summary>
            /// 当前页码
            /// </summary>
            public int CurrentPage { get; set; }
            /// <summary>
            /// 每页条数
            /// </summary>
            public int PageSize { get; set; }
            /// <summary>
            /// 总数量
            /// </summary>
            public int TotalCount { get; set; }
            /// <summary>
            /// 当前页码
            /// </summary>
            public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
            public List<T> Items { get; set; } = new List<T>();
            public object Channel { get; set; }
        }
        public PagedResult<T> GetPagedData<T>(List<T> query, int pageNumber, int pageSize)
        {
            var result = new PagedResult<T>
            {
                CurrentPage = pageNumber,
                PageSize = pageSize,
                TotalCount = query.Count(),
                Items = query.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList()
            };

            return result;
        }
    }
}
